<template>
  <div class="software-upgrade-container">
    <el-card
      v-loading="loading1"
      class="box-card"
      element-loading-text="请耐心等待......"
      shadow="never"
    >
      <div slot="header" class="config-header">日志等级设置</div>
      <div class="upgrade-instructions">
        <el-alert title="提示" type="warning" :closable="false" show-icon>
          <template slot="title">
            <div class="iconSize">{{ $t("logTip2") }}</div>
          </template>
        </el-alert>
      </div>
      <el-form ref="form" :model="logInfo" label-width="80px">
        <el-form-item label="日志等级">
          <el-select v-model="logInfo.logLevel" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="button-row">
        <el-col :span="3">
          <el-button type="primary" size="medium" @click="getLogLevel">刷新</el-button>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" size="medium" @click="setLogLevel">保存</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card
      v-loading="loading"
      class="box-card"
      :element-loading-text="loadingtxt"
      shadow="never"
    >
      <div slot="header" class="config-header">设备日志记录</div>
      <div class="upgrade-instructions">
        <el-alert title="提示" type="warning" :closable="false" show-icon>
          <template slot="title">
            <div class="iconSize">{{ $t("logTip1") }}</div>
          </template>
        </el-alert>
      </div>

      <el-row :gutter="10" class="button-row">
        <el-col :span="3">
          <el-button
            type="primary"
            size="medium"
            class="upgradeBtn"
            :disabled="isUploading"
            @click="doExport"
          >导出日志</el-button>
        </el-col>
        <el-col :span="8">
          <el-button
            type="danger"
            size="medium"
            class="upgradeBtn"
            @click="doClearLog"
          >清除日志</el-button>
        </el-col>
      </el-row>

    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      loading1: false,
      isUploading: false,
      loadingtxt: '',
      options: [],
      logInfo: {
        logLevel: null
      }
    }
  },
  created() {
    this.getLogLevel()
  },
  methods: {
    getLogLevel() {
      this.loading1 = true
      this.$store
        .dispatch('device/getLogLevel')
        .then((res) => {
          this.loading1 = false
          this.options = res.logLevelName
          this.logInfo.logLevel = res.logLevel
        })
        .catch((err) => {
          this.loading1 = false
          console.log(err)
        })
    },
    setLogLevel() {
      this.loading1 = true
      this.$store
        .dispatch('device/setLogLevel', this.logInfo)
        .then((res) => {
          this.loading1 = false
          this.$message.success('日志等级设置成功')
        })
        .catch((err) => {
          this.loading1 = false
          console.log(err)
        })
    },
    downloadFile(filename) {
    // 使用 axios 发起 GET 请求并设置 responseType 为 'blob'，以便处理文件内容
      this.$store.dispatch('device/getLogFile')
        .then((response) => {
          // 创建一个 Blob 对象
          const blob = new Blob([response], { type: 'text/plain' })

          // 创建一个指向 Blob 的 URL
          const blobUrl = window.URL.createObjectURL(blob)

          // 创建一个下载链接
          const a = document.createElement('a')
          a.href = blobUrl
          a.download = filename // 设置下载文件的名称

          // 模拟点击下载链接
          a.click()

          // 释放 Blob URL
          window.URL.revokeObjectURL(blobUrl)
          this.$message.success('导出日志成功')
        })
        .catch((error) => {
          this.$message.error('导出日志失败')
          console.error('文件下载失败:', error)
        })
    },

    exportLog() {
      this.loadingtxt = '导出中，请稍等......'
      this.loading = true
      this.isUploading = true
      this.$store
        .dispatch('device/exportLog')
        .then((res) => {
          this.loading = false
          this.isUploading = false
          if (res.code === 0) {
            this.downloadFile('hdlc_log.txt')
          } else {
            this.$message.error('导出日志失败')
          }
        })
        .catch((err) => {
          this.loading = false
          this.isUploading = false
          console.log(err)
        })
    },
    clearLog() {
      this.loadingtxt = '删除中，请稍等......'
      this.loading = true
      this.$store
        .dispatch('device/deleteLog')
        .then((res) => {
          this.loading = false
          if (res.code === 0) {
            this.$message.success('清除日志成功')
          } else {
            this.$message.error('清除日志失败')
          }
        })
        .catch((err) => {
          this.loading = false
          console.log(err)
        })
    },
    doExport() {
      this.$confirm('是否确认导出日志?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLog()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    doClearLog() {
      this.$confirm('是否确认清除日志?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.clearLog()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    }
  }
}
</script>

<style scoped>
.software-upgrade-container {
  padding: 20px;
}

.box-card {
  margin-bottom: 50px;
}

.config-header {
  font-size: 16px;
  font-weight: bold;
}

.upgrade-instructions {
  margin-bottom: 20px;
}

.upload-demo {
  margin-bottom: 20px;
}

.el-upload__text {
  font-size: 16px;
}

.el-upload__tip {
  color: #888;
}

.upgradeBtn {
  margin-top: 20px;
}
</style>
